
#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
    @author jay.han
"""

from cvirt.common.test import BaseTaskTest
from cvirt.tasks.vm import create_vm,start_vm
from cvirt.manager.models import Instance,Cluster

class VMTestCase(BaseTaskTest):

    def setUp(self):
        super(VMTestCase, self).setUp()

        self.instance = {
            "display_name":"test",
            "vcpu" : 1,
            "name":"i-%08.f" % 1L,
            "memory_mb" : 1024,
            "has_cdrom":False,
            "need_launch":False,
            "host":1,
            "sound_model":"ac97",
            "graphics_type":"vnc" }

        self.image = {
            "name":"test",
            "os_type":"WindowsXP"
        }
        self.networks = [{
                            "bridge":"br0",
                            "model":"virtio",
                            "type":"bridge"

                        }]
        self.disks = [{
                         "disk_name":"test1",
                         "disk_dev":"vda",
                         "disk_type":"disk",
                         "disk_bus":"virtio",
                         "disk_format":"qcow2",
                         "disk_size":8,
                         "disk_cache":None
                     }]

    def testAsyncCreate(self):
        Cluster(name='test',description='test',cpufamily='AMD').save()
        result = create_vm.apply_async(kwargs={"instance":self.instance,"networks":self.networks,
                                       "image":self.image,"disks":self.disks,"host":'ezzzehxx'},queue='ezzzehxx')
        result.get(timeout=30)
        self.assertEqual(result.successful(),True)
        self.assertEqual(result.status,'SUCCESS')


    def testAsyncStart(self):

        inst = Instance.objects.filter(status='shutdown')
        if inst:
            result = start_vm.apply_async(kwargs={"instance_id":inst[0].id,"host":'ezzzehxx'},queue='ezzzehxx')
            result.get(timeout=30)
            self.assertEqual(result.successful(),True)
            self.assertEqual(result.status,'SUCCESS')

    def testAsyncShutdown(self):
        inst = Instance.objects.filter(status='running')
        if inst:
            result = start_vm.apply_async(kwargs={"instance_id":inst[0].id,"host":'ezzzehxx'},queue='ezzzehxx')
            result.get(timeout=30)
            self.assertEqual(result.successful(),True)
            self.assertEqual(result.status,'SUCCESS')



